<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="queryForm" :model="listQuery" :rules="queryRules" label-position="left">
        <el-form-item label="" prop="title" style="float: left;">
          <el-input v-model="listQuery.title" placeholder="请输入标题" class="filter-item" style="width: 160px;margin-right:5px;"></el-input>
        </el-form-item>
        <el-form-item label="" prop="username" style="float: left;">
          <el-input v-model="listQuery.username" placeholder="请输入用户名" class="filter-item" style="width: 120px;margin-right:5px;"></el-input>
        </el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button v-waves class="filter-item" type="danger" @click="resetQueryForm('queryForm')">重置</el-button>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="list" size="mini" highlight-current-row style="width: 100%">
      <el-table-column width="300px" align="center" label="标题">
        <template slot-scope="{row}">
          <span>{{ row.Title }}</span>
        </template>
      </el-table-column>

      <el-table-column width="280px" align="center" label="摘要">
        <template slot-scope="{row}">
          <span>{{ row.Summary }}</span>
        </template>
      </el-table-column>

      <el-table-column width="310px" align="center" label="作者">
        <template slot-scope="{row}">
          <span>{{ row.AuthorName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="250px" label="创建日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.CreatedAt }}</span>
        </template>
      </el-table-column>

      <el-table-column width="250px" label="修改日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.UpdatedAt }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <router-link  class="el-button el-button--success el-button--mini" :to="'add/' + row.Id">
            编辑 <i class="el-icon-edit" />
          </router-link>
          <el-button
            type="primary"
            size="small"
            @click="handleDel(row.Id)"
          >
            下架
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleDel(row.Id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination style="margin-top: 1px; padding-top: 10px;" v-show="+total > 0" :total="total" :page.sync="listQuery.page_index" :limit.sync="listQuery.page_size" @pagination="getList" />
  </div>
</template>

<script>
  import {nodeCommentList, nodeCommentDetail, nodeCommentStatus, nodeCommentDel, nodeList} from '@/api/node'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination/index'

export default {
  name: 'NodeCommentList',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '0': '禁用',
        '1': '启用'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page_index: 1,
        page_size: 10,
        title: '',
        username: ''
      },
      queryRules: {
        title: [
          { required: false, message: '需要填写标题', trigger: 'blur' }
        ],
        username: [
          { required: false, message: '', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.resetQueryData()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      nodeCommentList(this.listQuery).then(response => {
        if (response.success) {
          this.list = response.data.data
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 1 * 300)
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
          setTimeout(() => {
            this.listLoading = false
          }, 1 * 100)
        }
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除权限,并删除角色已分配,用户已分配, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        nodeCommentDel(id).then((response) => {
          if (response.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: response.msg
            })
          }
        }).catch(err => {
          this.$message({
            type: 'success',
            message: '对不起,未知错误'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    resetQueryData() {
      this.listQuery = {
        page_index: 1,
        page_size: 10,
        title: '',
        username: ''
      }
    },
    resetQueryForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
